
import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session

#查询用户名是否存在
def judge_username(username):
    try:
        re = session.query(md.TbUser.User_Name,md.TbUser.User_PWD).filter(md.TbUser.User_Name == username).first()
        return re
    except:
        session.rollback()
        return False

# 获取所有用户名
def get_all_user():
    try:
        user_data = session.query(md.TbUser.User_Name, md.TbUser.IsQualified,md.TbUser.User_ID).all()
        return user_data
    except:
        print('查找用户失败')
        session.rollback()
        return False


# 获取所有管理员
def get_all_admin():
    try:
        admin_data = session.query(md.TbAdministrator.User_Name).all()
        return admin_data
    except:
        session.rollback()
        print('查找管理员失败')
        return False

#根据管理员获取法人id
def get_admin_leP_id(username):
    try:
        lep_id = session.query(md.TbAdministrator.LegalPerson_ID).filter(md.TbAdministrator.User_Name == username).first()
        print("查找管理员法人id成功")
        return lep_id
    except:
        print("查找管理员法人id失败")
        session.rollback()
        return False

# 获取管理员基本信息
def get_admin_inf():
    try:
        admin_inf = session.query(md.TbAdministrator.User_Name,
                                  md.TbAdministrator.Belong_Depart,
                                  md.TbAdministrator.LegalPerson_ID).all()
        return admin_inf
    except:
        session.rollback()
        print('查找管理员失败')
        return False

# 获取用户密码 系统库
def get_user_pwd(username):
    try:
        user_pwd = session.query(md.TbUser.User_PWD,md.TbUser.User_ID).filter(md.TbUser.User_Name == username).first()
        print("=--------------sql --------------")
        return user_pwd
    except:
        print('查找用户密码失败')
        session.rollback()
        return False

# 获取管理员密码
def get_admin_pwd(username):
    try:
        admin_pwd = session.query(md.TbAdministrator.User_PWD,md.TbAdministrator.User_ID).filter(md.TbAdministrator.User_Name == username).first()
        return admin_pwd
    except:
        print("查找管理员密码失败")
        session.rollback()
        return False


# 查询所有法人名称
def get_legalperson():
    try:
        legalperson_data = session.query(md.TbLicense.LegalPerson_ID, md.TbLicense.LegalPeson_Name).all()
        print(legalperson_data)
        return legalperson_data
    except:
        print('查找法人失败')
        session.rollback()
        return False


# 查询所有部门名称
def get_departname():
    try:
        departname_data = session.query(md.TbUserorgFramework.Depart_ID, md.TbUserorgFramework.Depart_Name).all()
        print(departname_data)
        return departname_data
    except:
        print('查找法人失败')
        session.rollback()
        return False



# if __name__ == '__main__':
#     # get_user_pwd(username='张三')
#     # get_all_admin()
#     get_departname()